package com.meepo.feed

import com.meepo.basic.schema.common.PageResult
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.basic.schema.feed.*
import com.meepo.sdk.component.http.RetrofitManager
import io.reactivex.Observable
import okhttp3.MultipartBody
import retrofit2.http.*

/**
 * @author
 *
 * @date  2020/10/27 11:11 PM
 * @version 1.0
 */
internal interface API {

    companion object {
        private var sInstance: API? = null
        fun instance(): API {
            if (sInstance == null) sInstance = RetrofitManager.create(API::class.java)
            return sInstance as API
        }

        const val SearchType_Main = "main"

        const val FeedCategory_Suggest = "suggest"
        const val FeedCategory_Video = "video"
        const val FeedCategory_Follow = "follow"
    }

    @GET("/api/v1/history/items")
    fun getHistory(
        @Query("action") actionType: String,
        @Query("page") page: Int = 1,
        @Query("size") size: Int
    ): Observable<ResponseWrapper<PageResult<Item>>>


    @POST("/resources/v1/files")
    fun uploadFiles(@Body body: MultipartBody): Observable<ResponseWrapper<List<String>>>

    @POST("/api/v1/feed/items")
    fun publish(@Body item: ItemReq): Observable<ResponseWrapper<Item>>

    @DELETE("/api/v1/feed/topic-relations")
    fun deleteTopicRelation(
        @Query("topicId") topicId: String
    ): Observable<ResponseWrapper<Any>>

    @POST("/api/v1/feed/topic-relations")
    fun addTopicRelation(
        @Query("topicId") topicId: String
    ): Observable<ResponseWrapper<Any>>

    @GET("/api/v1/feed/topics/{id}")
    fun fetchTopicDetail(
        @Path("id") id: String
    ): Observable<ResponseWrapper<Topic>>

    @GET("/api/v1/feed/topics")
    fun fetchTopics(
        @Query("keyword") keyword: String = "",
        @Query("authorId") authorId: String = "",
        @Query("joinedByUserId") joined: String = "",
        @Query("page") page: Int = 1,
        @Query("size") size: Int = 15
    ): Observable<ResponseWrapper<PageResult<Topic>>>

    @POST("/api/v1/feed/topics")
    fun addTopic(@Body req: TopicReq): Observable<ResponseWrapper<Topic>>

    @POST("/api/v1/feed/attitudes")
    fun addAttitude(
        @Query("itemId") itemId: String,
        @Query("itemType") itemType: String,
        @Query("attitudeId") attitudeId: String
    ): Observable<ResponseWrapper<Any>>

    @GET("/api/v1/feed/actions")
    fun fetchActions(
        @Query("itemType") itemType: String,
        @Query("actionType") actionType: String,
        @Query("authorUserId") authorUserId: String,
        @Query("targetUserId") targetUserId: String,
        @Query("page") page: Int = 1,
        @Query("size") size: Int = 15
    ): Observable<ResponseWrapper<PageResult<Action>>>


    @POST("/api/v1/feed/actions")
    fun addAction(
        @Query("itemId") itemId: String,
        @Query("itemType") itemType: String,
        @Query("actionType") actionType: String,
        @Query("actionId") actionId: String = "",
        @Query("cancel") cancel: Int = 0
    ): Observable<ResponseWrapper<Any>>


    @GET("/api/v1/feed/comments")
    fun fetchMainComments(
        @Query("itemId") targetId: String = "",
        @Query("authorId") authorId: String = "",
        @Query("search") search: String = "",
        @Query("page") page: Int = 1,
        @Query("size") size: Int = 15
    ): Observable<ResponseWrapper<PageResult<Comment>>>

    @GET("/api/v1/feed/comments/{commentId}/relies")
    fun fetchCommentReplies(
        @Path("commentId") commentId: String,
        @Query("page") page: Int = 1,
        @Query("size") size: Int = 15
    ): Observable<ResponseWrapper<PageResult<Comment>>>


    @GET("/api/v1/feed/comments/{id}")
    fun fetchCommentDetail(@Path("id") id: String): Observable<ResponseWrapper<Comment>>

    @POST("/api/v2/feed/comments")
    fun addComment(@Body commentReq: CommentReq): Observable<ResponseWrapper<Comment>>

    @GET("/api/v1/feed/items")
    fun fetch(
        @Query("category") category: String,
        @Query("topicId") topicId: String,
        @Query("keyword") keyword: String,
        @Query("authorId") authorId: String,
        @Query("action") action: String,
        @Query("page") page: Int,
        @Query("size") size: Int
    ): Observable<ResponseWrapper<PageResult<Item>>>

    @GET("/api/v2/feed/items")
    fun fetchFeedItemsV2(
        @Query("category") category: String = "",
        @Query("keyword") keyword: String = "",
        @Query("topicId") topicId: String = "",
        @Query("action") action: String = "",
        @Query("page") page: Int = 1,
        @Query("size") size: Int = 15
    ): Observable<ResponseWrapper<PageResult<Item>>>

    @GET("/api/v1/feed/mini-videos")
    fun fetchMiniVideos(@Query("category") category: String): Observable<ResponseWrapper<FeedResult>>

    @GET("/api/v1/feed/short-videos")
    fun fetchShortVideos(@Query("category") category: String): Observable<ResponseWrapper<PageResult<Item>>>

    @GET("/api/v1/feed/categories")
    fun fetchCategories(): Observable<ResponseWrapper<List<Category>>>

    @GET("/api/v1/feed/items/{id}")
    fun fetchDetail(@Path("id") id: String): Observable<ResponseWrapper<Item>>


    @GET("/api/v1/search/keywords")
    fun fetchSearchMetadata(): Observable<ResponseWrapper<SearchMetadata>>

    @GET("/api/v1/search/items")
    fun fetchByKeyword(@Query("keyword") keyword: String): Observable<ResponseWrapper<FeedResult>>

}